package huawei.exam1;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author : Rookie
 * @description :
 * @date : 2022/12/17
 */
public class HJ20 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            boolean verify = verifyPassword(str);
            if (verify) {
                System.out.println("OK");
            } else {
                System.out.println("NG");
            }
        }
    }

    private static boolean verifyPassword(String password) {
        if (password.length() <= 8) {
            return false;
        }

        // 密码长度及符号判断
        Map<String, Object> countMap = new HashMap<>();
        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);
            if (Character.isDigit(c)) {
                countMap.putIfAbsent("num",true);
            } else if (Character.isLowerCase(c)) {
                countMap.putIfAbsent("lowercase",true);
            } else if (Character.isUpperCase(c)) {
                countMap.putIfAbsent("uppercase",true);
            } else {
                countMap.putIfAbsent("others",true);
            }
        }
        if (countMap.size() < 3) {
            return false;
        }

        // 判断不能有长度大于2的包含公共元素的子串重复
        return !hasRepeatStr(password);
    }

    private static boolean hasRepeatStr(String str) {
        boolean flag = false;
        for (int i = 0; i < str.length() -3; i++) {
            String sub = str.substring(i, i + 3);
            String syStr = str.substring(i+1);
            if (syStr.contains(sub)) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
